package com.example.network.controller;

import com.example.network.common.Result;
import com.example.network.controller.request.FileRequest;
import com.example.network.controller.request.LoginRequest;
import com.example.network.model.entity.TimeTable;
import com.example.network.service.AdminService;
import com.example.network.service.TimeService;
import com.example.network.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author LR
 */
@Slf4j
@RestController
@RequestMapping("/admin")
@Tag(name = "admin接口 -黎")
public class AdminController   {
    @Resource
    private AdminService adminService;
    @Resource
    private TimeService timeService;
    @Resource
    private UserService userService;
    /**
     * 用户登录
     */
    @PostMapping("/login")
    @Operation(summary = "管理员登录")
    public Result login(@RequestBody LoginRequest request) {
        log.info("管理员{}进行了登录",request.getEmail());
        return Result.success(adminService.login(request));
    }

    /**
     *查询用户的登录记录
     */
    @GetMapping("/get")
    @ApiOperation(value = "查询单条AdminController数据", notes = "通过主键查询单条AdminController数据")
    @Operation(summary = "查询单个用户日志")
    public Result selectOneByUserName(@RequestParam("email") String email) {
        log.info("查询用户{}的数据",email);
        return Result.success(adminService.selectByEmail(email));
    }

    @GetMapping("/selectOneByAll")
    @Operation(summary = "查询所有用户日志")
    public Result selectOneByAll(){
        log.info("查询了所有用户日志");
        List<TimeTable> timeTables = adminService.selectOneByAll();
        return Result.success(timeTables);
    }

    @DeleteMapping("/deleteUser")
    @Operation(summary = "删除用户")
    public Result deleteUser(@RequestParam String email){
        log.info("管理员删除了用户{}",email);
        userService.DeleteByUserEmail(email);
        timeService.insertTime(email,LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm")),"被管理员拉黑");
        return Result.success("用户"+email+"拉黑成功");
    }

    @GetMapping("/selectAllFile")
    @Operation(summary = "查看所有文件")
    public Result selectAllFile(){
        log.info("管理员查看了所有文件");
        List<FileRequest> request = adminService.selectAllFile();
        return Result.success(request);
    }

    @GetMapping("/deleteByAll")
    @Operation(summary = "清空所有用户日志")
    public Result deleteByAll(){
        timeService.deleteByAll();
        log.info("管理员清空了用户日志");
        return Result.success("清空成功");
    }
}
